﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2013
 * 
 * 文件名：FILE_NAME
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：ifunnyface@outlook.com
 * 日 期：2013-01-07 15:06:22
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Transactions;
using Common;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using IDataAccess;
using IManager;

namespace Manager
{

    /// <summary>
    /// 
    /// </summary>
    public class ManagerProduct : IManagerProduct
    {

        /// <summary>
        /// 用户信息数据访问接口
        /// </summary>
        public IDataAccessPartition<ProductPartitionInfo> IDAPartition { get; set; }



        /// <summary>
        /// 产品数据访问接口
        /// </summary>
        public IDataAccessProduct IDAProduct { get; set; }


        #region 产品分类

        /// <summary>
        /// 通过Id获取分类
        /// </summary>
        /// <param name="partitionId">Id</param>
        /// <returns>分类信息</returns>
        public ProductPartitionInfo GetPartition(string partitionId)
        {
            return IDAPartition.Find(partitionId);
        }


        /// <summary>
        /// 获取最大编号
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <returns>编号</returns>
        public string Max(string prefix)
        {
            var p = PredicateExtension.True<ProductPartitionInfo>();
            p = p.And(e => e.PartitionId.StartsWith(prefix));
            return IDAPartition.Filter(p).Max(e => e.PartitionId.Substring(prefix.Length));
        }


        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns>分类列表</returns>
        public IList<ProductPartitionInfo> GetPartitions()
        {
            return IDAPartition.LoadAll().ToList();
        }


        /// <summary>
        /// 添加分类信息
        /// </summary>
        /// <param name="partitionInfo">分类信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool AddPartition(ProductPartitionInfo partitionInfo)
        {
            string guid = GuidHelper.GetGuidNo(Guid.NewGuid());
            //const int newId = 1;
            var prefixPid = Prefix(partitionInfo.ParentId);
            var p = IDAPartition.Find(partitionInfo.ParentId);
            //var id = Max(pid);
            //if (string.IsNullOrEmpty(id))
            //{
            //    id = Convert.ToString(newId);
            //}
            //else
            //{
            //    id = Convert.ToString(Convert.ToInt32(id) + newId);
            //}


            var pid = prefixPid + guid.Substring(0,8);

            if(IDAPartition.Find(pid) != null)
            {
                pid = prefixPid + GuidHelper.GetGuidNo(Guid.NewGuid()).Substring(0,8);
            }

            partitionInfo.PartitionId = pid;
            partitionInfo.Level = p.Level + 1;
            IDAPartition.Create(partitionInfo);
            p.HasChildren = 1;
            IDAPartition.Update(p);
            IDAPartition.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 判定前缀
        /// </summary>
        /// <param name="str"></param>
        /// <returns>前缀</returns>
        string Prefix(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                throw new ArgumentNullException();
            }
            str = str.ToUpper();
            var strTemp = string.Empty;
            if (str.StartsWith("BH"))
            {
                strTemp = "BH";
            }
            else if (str.StartsWith("FW"))
            {
                strTemp = "FW";
            }
            else if (str.StartsWith("FZ"))
            {
                strTemp = "FZ";
            }
            else if (str.StartsWith("GYP"))
            {
                strTemp = "GYP";
            }
            else if (str.StartsWith("SP"))
            {
                strTemp = "SP";
            }
            else
            {
                strTemp = "YCL";
            }
            return strTemp;
        }


        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="partitionId">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeletePartition(string partitionId)
        {
            IDAPartition.Delete(partitionId);
            IDAPartition.UnitOfWork.Commit();
            return true;
        }



        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="partitionIds">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeletePartition(IList<string> partitionIds)
        {
            foreach (var partitionId in partitionIds)
            {
                var p = GetPartition(partitionId);
                if (p != null && p.Level > 0)
                {
                    var parent = p.ProductPartition;
                    IDAPartition.Delete(partitionId);
                    if (parent.Children == null || parent.Children.Count == 0)
                    {
                        parent.HasChildren = 0;
                        IDAPartition.Update(parent);
                    }
                }
            }
            IDAPartition.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 更新分类信息
        /// </summary>
        /// <param name="partitionInfo">分类信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool UpdatePartition(ProductPartitionInfo partitionInfo)
        {
            IDAPartition.Update(partitionInfo);
            IDAPartition.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<ProductPartitionInfo> LoadAll(Expression<Func<ProductPartitionInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<ProductPartitionInfo>[] orderByExpressions)
        {
            return IDAPartition.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        public int LoadCount(Expression<Func<ProductPartitionInfo, bool>> where)
        {
            return IDAPartition.Count(where);
        }


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<ProductPartitionInfo> LoadAll(Expression<Func<ProductPartitionInfo, bool>> where, params IOrderByExpression<ProductPartitionInfo>[] orderByExpressions)
        {
            return IDAPartition.Filter(where, orderByExpressions).ToList();
        }

        #endregion



        #region 产品

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="product">产品信息</param>
        /// <returns>bool</returns>
        public bool AddProduct(ProductInfo product)
        {
            IDAProduct.Create(product);
            IDAProduct.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<ProductInfo> LoadProductAll(Expression<Func<ProductInfo, bool>> where,
                                                    int pageIndex,
                                                    int pageSize,
                                                    params IOrderByExpression<ProductInfo>[] orderByExpressions)
        {
            return IDAProduct.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        public int LoadProductCount(Expression<Func<ProductInfo, bool>> where)
        {
            return IDAProduct.Count(where);
        }


        /// <summary>
        /// 通过Id获取分类
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns>分类信息</returns>
        public ProductInfo GetProduct(int Id)
        {
            return IDAProduct.Find(Id);
        }



        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeleteProduct(int id)
        {
            IDAProduct.Delete(id);
            IDAProduct.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="ids">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeleteProduct(IList<int> ids)
        {
            foreach (var id in ids)
            {
                IDAProduct.Delete(id);
            }
            IDAProduct.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 更新分类信息
        /// </summary>
        /// <param name="info">分类信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool UpdateProduct(ProductInfo info)
        {
            IDAProduct.Update(info);
            IDAProduct.UnitOfWork.Commit();
            return true;
        }


        #endregion
    }

}